package services;

//import java.util.Collection;

//import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import t4p.model.Tweet;
import t4p.model.User;
import t4p.persistence.DaoFactory;

public class TweetService {
	private static TweetService instance = new TweetService();
	private TweetService(){}
	
	public static TweetService getInstace(){
		return instance;
	} 
	
	public static void addTweet(User loggedUser, String tweetText, Tweet tweetPrevious){
		Date fecha = new Date();
		Tweet myTweet = new Tweet();
		myTweet.setId(TweetService.maxTweetID());
		myTweet.setText(tweetText);
		myTweet.setAuthor(loggedUser);
		myTweet.setTimestamp(fecha);
		myTweet.setMentions(TweetService.getMentionsForTweetText(tweetText));
		myTweet.setPrevious(tweetPrevious);
		DaoFactory.getInstance().getTweetDao().insert(myTweet);

		//Collection<Tweet> tweetList = DaoFactory.getInstance().getTweetDao().findAll();
		
	}
	
	public static Collection<Tweet> getTweetByUser(User loggedUser){
		Collection<Tweet> allTweetsByUser = new LinkedList<Tweet>();
		
		for(Tweet item:DaoFactory.getInstance().getTweetDao().findAll()){
			User tweetAuthor =  (User)item.getAuthor();
			if (tweetAuthor.getUsername().equals(loggedUser.getUsername())){
				allTweetsByUser.add(item);
			}
		}
		return allTweetsByUser;
	}
	
	public static Collection<Tweet> getFollowingTweets(User user){

		Collection<Tweet> allFollowingTweets = new LinkedList<Tweet>();

		for(User item:user.getFollowing()){
			allFollowingTweets.addAll(getTweetByUser(item));
		}
		
		return allFollowingTweets;
	}
	
	public static Collection<Tweet> getFollowersTweets(User user){

		Collection<Tweet> allFollowerTweets = new LinkedList<Tweet>();

		for(User item:user.getFollowers()){
			allFollowerTweets.addAll(getTweetByUser(item));
		}
		
		return allFollowerTweets;
	}
	
	private static long maxTweetID (){
		long maxID = 0;
		
		for(Tweet item:DaoFactory.getInstance().getTweetDao().findAll()){
			maxID += item.getId();
		}
		
		return maxID;
	}
	
	public static List<User>getMentionsForTweetText(String tweetText){
		List<User> mentionsList = new ArrayList<User>();
		
		for(User item:DaoFactory.getInstance().getUserDao().findAll()){
			String arrobaUser = "@"+item.getUsername();
			if(tweetText.indexOf(arrobaUser)!=-1){
				mentionsList.add(item);
			}
		}
		
		return mentionsList;
	}
	
	public static Tweet getTweetByID(String tweetID){
		Tweet tweet;
		Long id = Long.valueOf(tweetID);
		tweet = DaoFactory.getInstance().getTweetDao().findById(id);
		
		return tweet;
	}
	
	public static LinkedList <Tweet> getConversation(String tweetID){
		Tweet tweetAct = getTweetByID(tweetID);
		LinkedList <Tweet> conversationList = new LinkedList<Tweet>();
		conversationList.add(tweetAct);
		
		while(tweetAct.getPrevious()!=null){
			conversationList.add(tweetAct.getPrevious());
			tweetAct = tweetAct.getPrevious();
		}
		
	Collections.reverse(conversationList);
	
	return conversationList;
	}
	
	public static LinkedList <Tweet> getMentionsAboutMe (User loggedUser){
		LinkedList <Tweet> mentionsList = new LinkedList<Tweet>();
		
		Predicate userPredicate = new UserPredicate(loggedUser.getUsername());
		Collection<Tweet> tweetList = DaoFactory.getInstance().getTweetDao().findAll();
		
		for(Tweet item:tweetList){
			User userMentionated = (User)CollectionUtils.find(item.getMentions(), userPredicate);
			
			if(userMentionated != null){
				mentionsList.add(item);
			}
		
		}
	
		return mentionsList;
	}
	
}

